package com.example.airport;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.Switch;
import android.widget.Toast;

import com.example.airport.net.G;
import com.example.airport.parent.FatherActivity;
import com.shawnlin.preferencesmanager.PreferencesManager;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.everything.android.ui.overscroll.OverScrollDecoratorHelper;

public class ConfigActivity extends FatherActivity {

    @BindView(R.id.username_text)
    EditText username_text;

    @BindView(R.id.password_text)
    EditText password_text;

    @BindView(R.id.topic_text)
    EditText topic_text;

    @BindView(R.id.server_text)
    EditText server_text;


    @BindView(R.id.time_text)
    EditText time_text;


    @BindView(R.id.pilot_switch_button)
    Switch pilot_switch_button;


    @BindView(R.id.craft_switch_button)
    Switch craft_switch_button;

    @BindView(R.id.appkey_text)
    EditText appkey_text;

    @BindView(R.id.secret_text)
    EditText secret_text;

    @BindView(R.id.robotid_text)
    EditText robotid_text;

    @BindView(R.id.clientid_text)
    EditText clientid_text;

    @BindView(R.id.sn_text)
    EditText sn_text;


    @BindView(R.id.nomatch_text)
    EditText nomatch_text;

    @OnClick(R.id.ok_button)
    void saveConfig()
    {
        String username=username_text.getText().toString();
        String password=password_text.getText().toString();
        String topic=topic_text.getText().toString();
        String server=server_text.getText().toString();
        String time=time_text.getText().toString();
        Boolean pilot=pilot_switch_button.isChecked();
        Boolean craft=craft_switch_button.isChecked();
        String appkey=appkey_text.getText().toString();
        String secret=secret_text.getText().toString();
        String robotid=robotid_text.getText().toString();
        String clientid=clientid_text.getText().toString();
        String sn=sn_text.getText().toString();
        String nomatch=nomatch_text.getText().toString();


        PreferencesManager.putString("username", username);
        PreferencesManager.putString("password", password);
        PreferencesManager.putString("topic_pilot", topic);
        PreferencesManager.putString("server", server);
        PreferencesManager.putString("time", time);
        PreferencesManager.putBoolean("pilot", pilot);
        PreferencesManager.putBoolean("craft", craft);
        PreferencesManager.putString("appkey", appkey);
        PreferencesManager.putString("secret", secret);
        PreferencesManager.putString("robotid", robotid);
        PreferencesManager.putString("clientid", clientid);
        PreferencesManager.putString("sn", sn);
        PreferencesManager.putString("nomatch", nomatch);

        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    int model=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config);

        Bundle bundle = getIntent().getExtras();
        model = bundle.getInt("model");


        ButterKnife.bind(this);
        setStatusBarView();
        initToolbar();
        getConfig();


    }


    void getConfig()
    {


        String username=PreferencesManager.getString("username",G.USERNAME);
        String password=PreferencesManager.getString("password",G.PASSWORD);
        String topic=PreferencesManager.getString("topic",G.TOPIC_PILOT);
        String server=PreferencesManager.getString("server",G.BROKER_URL);
        String time=PreferencesManager.getString("time",G.TIME);
        Boolean pilot=PreferencesManager.getBoolean("pilot",G.PILOT);
        Boolean craft=PreferencesManager.getBoolean("craft",G.CRAFT);

        String appkey=PreferencesManager.getString("appkey",G.APPKEY);
        String secret=PreferencesManager.getString("secret",G.SECRET);
        String robotid=PreferencesManager.getString("robotid",G.ROBOTID);
        String clientid=PreferencesManager.getString("clientid",G.CLIENTID);
        String sn=PreferencesManager.getString("sn",G.SN);
        String nomatch=PreferencesManager.getString("nomatch",G.NOMATCH);


        username_text.setText(username);
        password_text.setText(password);
        topic_text.setText(topic);
        server_text.setText(server);
        //time_text.setText(time);
        time_text.setText(this.model+"");
        appkey_text.setText(appkey);
        secret_text.setText(secret);
        robotid_text.setText(robotid);
        clientid_text.setText(clientid);
        sn_text.setText(sn);
        nomatch_text.setText(nomatch);
        pilot_switch_button.setChecked(pilot);
        craft_switch_button.setChecked(craft);

    }


}